package com.dzirtbry.miner.controller;

import com.dzirtbry.miner.model.Cell;
import com.dzirtbry.miner.model.Pane;
import com.dzirtbry.miner.settings.GameSettings;

import java.awt.*;

import static com.dzirtbry.miner.controller.GameState.*;

/**
 * User: dzirtbry
 * Date: 2/26/12
 * Time: 4:45 PM
 */
public class PaneController {
    private Pane pane;
    private GameSettings gameSettings;
    private GameState state;

    public void changeFlag(int row, int col) {
        Cell c = pane.getCell(row, col);
        c.setFlagged(!c.isFlagged());
    }

    public void openCell(int row, int col) {
        switch (state) {
            case BEFORE_START:
                pane = Pane.generatePane(gameSettings.getPaneDimension(), gameSettings.getMinesNumber(), row, col);
                pane.getCell(row, col).setOpened(true);
                state = IN_PROGRESS;
                break;
            case IN_PROGRESS:
                Cell c = pane.getCell(row, col);
                if (c.isMined() && !c.isFlagged()) {
                    c.setOpened(true);
                    state = LOSE;
                    break;
                }
                openRecursively(row, col);
                break;
            default:
                break;
        }
        checkState();
    }

    private void openRecursively(int r, int c) {
        Dimension d  = gameSettings.getPaneDimension();
        Cell cell = pane.getCell(r, c);
        cell.setOpened(true);
        if (cell.getNumber() == 0) {
            if (r>0) {
                openRecursively(r - 1, c);
                if (c>0) {
                    openRecursively(r - 1, c - 1);
                }
                if (c<d.width-1) {
                    openRecursively(r - 1, c + 1);
                }
            }
            if (r<d.height-1) {
                openRecursively(r + 1, c);
                if (c>0) {
                    openRecursively(r + 1, c - 1);
                }
                if (c<d.width-1) {
                    openRecursively(r + 1, c + 1);
                }
            }
            if (c>0) {
                openRecursively(r, c - 1);
            }
            if (c<d.width-1) {
                openRecursively(r, c + 1);
            }
        }
    }

    private void checkState() {
        int fullSize = gameSettings.getPaneDimension().height * gameSettings.getPaneDimension().width;
        int unopened = 0;
        for (int i = 0; i < gameSettings.getPaneDimension().height; i++) {
            for (int j = 0; j < gameSettings.getPaneDimension().width; j++) {
                unopened += pane.getCell(i, j).isOpened() ? 1 : 0;
            }
        }

        if (fullSize - unopened == gameSettings.getMinesNumber()) {
            state = WON;
        }
    }

    public void setPane(Pane pane) {
        this.pane = pane;
    }

    public Pane getPane() {
        return pane;
    }

    public GameState getState() {
        return state;
    }

    public void setState(GameState state) {
        this.state = state;
    }

    public GameSettings getGameSettings() {
        return gameSettings;
    }

    public void setGameSettings(GameSettings gameSettings) {
        this.gameSettings = gameSettings;
    }
}
